#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QtDebug>
#include <QSqlRecord>
#include <QAction>
#include <QTableWidgetItem>
#include <QSqlDriver>


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , db(QSqlDatabase::addDatabase("QSQLITE"))
    , sq(db)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    db.setDatabaseName("test.db");
    if(db.open())
    {
        qDebug() << "数据库打开成功";
    }
    else
    {
        qDebug() << "数据库打开失败";
        return;
    }
    qDebug() << sq.exec(R"(create table if not exists test (name text, age int);)");

    sq.exec("select * from test");

    ui->tableWidget->setColumnCount(sq.record().count());
    ui->tableWidget->setHorizontalHeaderLabels(QStringList() << "name" << "age");


    qDebug() << "当前数据库是否支持QSqlQuery::size函数:" << db.driver()->hasFeature(QSqlDriver::QuerySize);
    qDebug() << sq.size();

    qDebug() << "first" << sq.isValid();
    qDebug() << "sssssss" << sq.at();

    sq.last();
    qDebug() << sq.at();
    ui->tableWidget->setRowCount(sq.at() + 1);
    sq.first();

    qDebug() << "second" << sq.isValid();

    while(sq.next())
    {
        QString tmp = sq.record().value(0).toString();
        qDebug() << tmp;
        QTableWidgetItem *tb = new QTableWidgetItem (tmp);
//        ui->tableWidget->setItem(sq.at() - 1, 0, tb);
    }


//    QAction *ac = new QAction(this);
//    ac->setText("测试");
//    ui->tableWidget->addAction(ac);

//    ui->tableWidget.
}

MainWindow::~MainWindow()
{
    db.close();
    delete ui;
}

